home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / beans / simplebean / example / Acme07Bean.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  4.1 KB  |  153 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. package acme.beans;
  18.  
  19. import java.awt.*;
  20. import java.awt.event.*;
  21. import java.io.Serializable;
  22. import java.util.Vector;
  23.  
  24.  
  25. public class Acme07Bean extends Canvas implements Serializable {
  26.  
  27.  
  28.     public Acme07Bean() {
  29.         this("AcmeBean Serial# 07");
  30.     }
  31.  
  32.     public Acme07Bean(String label) {
  33.         super();
  34.         this.label = label;
  35.         setFont(new Font("Dialog", Font.PLAIN, 12));
  36.     }
  37.  
  38.     public synchronized void paint(Graphics g) {
  39.  
  40.         int width = getSize().width;
  41.         int height = getSize().height;
  42.  
  43.         g.setColor(beanColor);
  44.         g.fillRect(1, 1, width - 2, height - 2);
  45.         g.draw3DRect(0, 0, width - 1, height - 1, true);
  46.  
  47.         g.setColor(getForeground());
  48.         g.setFont(getFont());
  49.  
  50.         g.drawRect(2, 2, width - 4, height - 4);
  51.  
  52.         FontMetrics fm = g.getFontMetrics();
  53.         g.drawString(label, (width - fm.stringWidth(label)) / 2, 
  54.                           (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2);
  55.     }
  56.  
  57.     public void fireAction() {
  58.         if (debug) {
  59.             System.err.println("Button " + getLabel() + " pressed.");
  60.         }
  61.         Vector targets;
  62.         synchronized (this) {
  63.             targets = (Vector) listeners.clone();
  64.         }
  65.         ActionEvent actionEvt = new ActionEvent(this, 0, null);
  66.         for (int i = 0; i < targets.size(); i++) {
  67.             ActionListener target = (ActionListener)targets.elementAt(i);
  68.             target.actionPerformed(actionEvt);
  69.         }
  70.  
  71.         Component parent = getParent();
  72.         if (parent != null) {
  73.             parent.postEvent(new Event(this, Event.MOUSE_DOWN, null));
  74.         }
  75.     }
  76.  
  77.     public void setDebug(boolean x) {
  78.         boolean old = debug;
  79.         debug = x;
  80.     }
  81.  
  82.     public boolean getDebug() {
  83.         return debug;
  84.     }
  85.  
  86.     public Color getColor() {
  87.         return beanColor;
  88.     }
  89.  
  90.     public void setColor(Color newColor) {
  91.         beanColor = newColor;
  92.         repaint();
  93.     }
  94.  
  95.     public String getLabel() {
  96.         return label;
  97.     }
  98.  
  99.     public void setLabel(String newLabel) {
  100.         String oldLabel = label;
  101.         label = newLabel;
  102.         sizeToFit();
  103.     }
  104.  
  105.     public Dimension getPreferredSize() {
  106.         FontMetrics fm = getFontMetrics(getFont());
  107.         return new Dimension(fm.stringWidth(label) + TEXT_XPAD, 
  108.                              fm.getMaxAscent() + fm.getMaxDescent() + TEXT_YPAD);
  109.     }
  110.  
  111.     public Dimension getMinimumSize() {
  112.         return getPreferredSize();
  113.     }
  114.  
  115.     private void sizeToFit() {
  116.         Dimension d = getPreferredSize();
  117.         setSize(d.width, d.height);
  118.         Component p = getParent();
  119.         if (p != null) {
  120.             p.invalidate();
  121.             p.doLayout();
  122.         }
  123.     }
  124.  
  125.     public boolean handleEvent(Event evt) {
  126.         if (! isEnabled()) {
  127.             return false;
  128.         }
  129.         switch (evt.id) {
  130.         case Event.MOUSE_UP:
  131.             fireAction();
  132.             return true;
  133.         }
  134.         return false;
  135.     }
  136.  
  137.     public synchronized void addActionListener(ActionListener l) {
  138.         listeners.addElement(l);
  139.     }
  140.  
  141.     public synchronized void removeActionListener(ActionListener l) {
  142.         listeners.removeElement(l);
  143.     }
  144.  
  145.  
  146.     private boolean debug = true;
  147.     private Color beanColor = Color.cyan;
  148.     private Vector listeners = new Vector();
  149.     private String label;
  150.     static final int TEXT_XPAD = 12;
  151.     static final int TEXT_YPAD = 8;
  152. }
  153.